(function($) {
	

	jQuery(function() {

		var dankov_sticky_menu_offset_top = jQuery('.navbar').offset().top;

		var dankov_sticky_menu = function() {
			var scroll_top = jQuery(window).scrollTop();

			if (scroll_top > dankov_sticky_menu_offset_top) {
				jQuery('.menu').addClass('sticky-menu');
			} else {
				jQuery('.menu').removeClass('sticky-menu');
			}
		};

		dankov_sticky_menu();

		jQuery(window).scroll(function() {
			dankov_sticky_menu();
		});
		
		
		/*  Dropdown menu animation
		/* ------------------------------------ */
		$('.nav ul.sub-menu').hide();
		$('.nav li').hover( 
			function() {
				$(this).children('ul.sub-menu').slideDown('fast');
			}, 
			function() {
				$(this).children('ul.sub-menu').hide();
			}
		);
		
		/*  Mobile menu smooth toggle height
		/* ------------------------------------ */	
		$('.nav-toggle').on('click', function() {
			slide($('.nav-wrap .nav', $(this).parent()));
		});
		
		function slide(content) {
			var wrapper = content.parent();
			var contentHeight = content.outerHeight(true);
			var wrapperHeight = wrapper.height();
		 
			wrapper.toggleClass('expand');
			if (wrapper.hasClass('expand')) {
				setTimeout(function() {
					wrapper.addClass('transition').css('height', contentHeight);
				}, 10);
			} else {
				setTimeout(function() {
					wrapper.css('height', wrapperHeight);
					setTimeout(function() {
					wrapper.addClass('transition').css('height', 0);
					}, 10);
				}, 10);
			}

			wrapper.one('transitionEnd webkitTransitionEnd transitionend oTransitionEnd msTransitionEnd', function() {
				if(wrapper.hasClass('open')) {
					wrapper.removeClass('transition').css('height', 'auto');
				}
			});
		}
		
		
		var textObject = {	
				delay : 300,
				effect : 'replace',
				classColour : 'blue',
				flash : function(obj, effect, delay) {
					if (obj.length > 0) {
						if (obj.length > 1) {
							jQuery.each(obj, function() {
								effect = effect || textObject.effect;
								delay = delay || textObject.delay;
								textObject.flashExe($(this), effect, delay);					
							});
						} else {
							effect = effect || textObject.effect;
							delay = delay || textObject.delay;
							textObject.flashExe(obj, effect, delay);
						}
					}
				},
				flashExe : function(obj, effect, delay) {
					var flash = setTimeout(function() {
						switch(effect) {
							case 'replace':
							obj.toggle();
							break;
							case 'colour':
							obj.toggleClass(textObject.classColour);
							break;
						}
						textObject.flashExe(obj, effect, delay);
					}, delay);
				}
			};
		
		//3D text plugin
		// jQuery plugin definition
		$.fn.text3d = function(opts) {
		
			// default configuration
			var config = $.extend({}, {
				depth: 5,
				angle: 100,
				color: "#ccc",
				lighten: -0.15,
				shadowDepth: 10,
				shadowAngle: 80,
				shadowOpacity: 0.3
			}, opts);
			
			// to radians
			config.angle = config.angle * Math.PI / 180;
			config.shadowAngle = config.shadowAngle * Math.PI / 180;
			
			// create text shadow
			function TextShadow(e) {
			
				var s = "", i, f, x, y, c;
				
				// 3D effect
				for (i = 1; i <= config.depth; i++) {
					x = Math.round(Math.cos(config.angle) * i);
					y = Math.round(Math.sin(config.angle) * i);
					c = ColorLuminance(config.color, (i-1)/(config.depth-1)*config.lighten);
					s += x+"px "+y+"px 0 "+c+",";
				}
				
				// shadow
				for (f = 1, i = 1; f <= config.shadowDepth; i++) {
					f = f+i;
					x = Math.round(Math.cos(config.shadowAngle) * f);
					y = Math.round(Math.sin(config.shadowAngle) * f);
					c = config.shadowOpacity - ((config.shadowOpacity - 0.1) * i/config.shadowDepth);
					s += x+"px "+y+"px "+f+"px rgba(0,0,0,"+c+"),";
				}
				
				e.style.textShadow = s.substr(0, s.length-1);		
			}
			
			
			// return lighter (+lum) or darker (-lum) color
			function ColorLuminance(hex, lum) {

				// validate hex string
				hex = String(hex).replace(/[^0-9a-f]/gi, '');
				if (hex.length < 6) {
					hex = hex[0]+hex[0]+hex[1]+hex[1]+hex[2]+hex[2];
				}
				lum = lum || 0;
				
				// convert to decimal and change luminosity
				var rgb = "#", c, i;
				for (i = 0; i < 3; i++) {
					c = parseInt(hex.substr(i*2,2), 16);
					c = Math.round(Math.min(Math.max(0, c + (c * lum)), 255)).toString(16);
					rgb += ("00"+c).substr(c.length);
				}

				return rgb;
			}

			
			// initialization
			this.each(function() {
				TextShadow(this);
			});

			return this;
		};
		
		jQuery(".flash-colour").text3d();
		textObject.flash($('.flash-colour'), 'colour', 400);
		//end text
		//gallery
		/*var firstImage = jQuery('#flexslider-featured').find('img').filter(':first'),
		checkforloaded = setInterval(function() {
			var image = firstImage.get(0);
			if (image.complete || image.readyState == 'complete' || image.readyState == 4) {
				clearInterval(checkforloaded);

				jQuery('#flexslider-featured').flexslider({
					animation: "slide",
					useCSS: false, // Fix iPad flickering issue
					slideshow: true,
					directionNav: false,
					controlNav: false,
					pauseOnHover: true,
					slideshowSpeed: 3000,
					animationSpeed: 400,
					smoothHeight: true,
					touch: false
				});

			}
		}, 20);*/
		$(window).load(function() {
			$('.flexslider').flexslider({
				animation: "slide",
				slideshow: true,
				slideshowSpeed: 3000,
				animationSpeed: 400,
				controlNav: true,
				touch: false,
			});
		});
		
		//end gallery
		/*  Toggle header search
		/* ------------------------------------ */
		$('.toggle-search').click(function(){
			$('.toggle-search').toggleClass('active');
			$('.search-expand').fadeToggle(250);
	            setTimeout(function(){
	                $('.search-expand input').focus();
	            }, 300);
		});
		
		/*  Scroll to top
		/* ------------------------------------ */
		jQuery(window).scroll(function(){
			if (jQuery(this).scrollTop() > 100) {
				jQuery('.scrollup').fadeIn();
			} else {
				jQuery('.scrollup').fadeOut();
			}
		}); 

		jQuery('.scrollup').click(function(){
			jQuery("html, body").animate({ scrollTop: 0 }, 700);
			return false;
		});
		
		/*  Sidebar collapse
		/* ------------------------------------ */
		$('body').addClass('s1-collapse');
		$('body').addClass('s2-collapse');
		$('.s1 .sidebar-toggle').click(function(){
			$('body').toggleClass('s1-collapse').toggleClass('s1-expand');
			if ($('body').is('.s2-expand')) { 
				$('body').toggleClass('s2-expand').toggleClass('s2-collapse');
			}
		});
		$('.s2 .sidebar-toggle').click(function(){
			$('body').toggleClass('s2-collapse').toggleClass('s2-expand');
			if ($('body').is('.s1-expand')) { 
				$('body').toggleClass('s1-expand').toggleClass('s1-collapse');
			}
		});
	});

})(jQuery);

	